import unittest
import requests
from api.RegLoginApi import RegLoginApi
from api.InvestApi import InvestApi
from common.read_html_util import html_util
from parameterized import parameterized
from common.resd_json import read_json


class TestInvest(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.ses = requests.session()
        # 实例化登录模块封装的类
        self.reg_api = RegLoginApi(self.ses)
        # 实例化投资模块封装的类
        self.invest_api = InvestApi(self.ses)

    def tearDown(self) -> None:
        # 关闭session对象
        self.ses.close()

    # 获取投资测试用例
    @parameterized.expand(read_json("test004_data", "api_data"))
    def test01_get_invest(self,id,depositCertificate,amount,expect_status_code,expect_status,expect_description):
        # 登录
        RegLoginApi(self.ses).user_login("18373444511", "11111MM")
        resp = InvestApi(self.ses).get_investment(id, depositCertificate, amount)
        print(resp.json())
        # 调用投资接口
        self.assertEqual(expect_status_code, resp.status_code)
        self.assertEqual(expect_status, resp.json().get("status"))
        self.assertIn(expect_description, resp.json().get("description"))

    # 第三方投资测试用例
    def test02_the_third_invest(self):
        RegLoginApi(self.ses).user_login("18373444511", "11111MM")
        resp = InvestApi(self.ses).get_investment(642, -1, 100)
        url = html_util(resp)[0]
        body = html_util(resp)[1]
        resp=InvestApi(self.ses).the_third_investment(url, body)
        self.assertEqual(200, resp.status_code)
        self.assertIn("OK", resp.text)
